use crypto::{hmac,sha2};
use mlua::{UserData, UserDataMethods};
use crypto::mac::Mac;

#[derive(Default)]
pub struct Hash{
    key:Vec<u8>
}
impl Hash{
    pub fn new(key:Vec<u8>)->Self{
        Hash{key}
    }
}

impl UserData for Hash{
    fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
        methods.add_method_mut("hmac_encode",|_,hash,data:Vec<u8>|{
            let disget = sha2::Sha256::new();
            let mut hm = hmac::Hmac::new(disget, hash.key.as_slice());
            hm.input(data.as_slice());
            let res = hm.result();
            Ok(res.code().to_vec())
        })
    }
}